package com.asset.dispoal.file.replyDetail.domain.service;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.asset.dispoal.file.replyDetail.api.FileReplyDetailService;
import com.asset.dispoal.file.replyDetail.domain.dto.ReplyDetailExportTemplate;
import com.asset.dispoal.file.replyDetail.domain.service.command.FileReplyDeleteCommand;
import com.asset.dispoal.file.replyDetail.domain.service.command.FileReplyDetailExcelDataSaveCommand;
import com.asset.dispoal.file.replyDetail.domain.service.command.FileReplyDetailMonthStatisticsCommand;
import com.asset.dispoal.file.replyDetail.domain.service.command.FileReplyDetailYearStatisticsCommand;
import com.asset.dispoal.file.replyDetail.domain.service.command.FileReplyExportCommand;
import com.asset.dispoal.file.replyDetail.domain.service.command.FileReplyModifyCommand;
import com.asset.dispoal.file.replyDetail.domain.service.command.FileReplyPageCommand;
import com.asset.dispoal.file.replyDetail.domain.service.command.FileReplySaveCommand;
import com.asset.dispoal.file.replyDetail.domain.service.excelListener.ReplyDetailExcelListener;
import com.asset.dispoal.file.replyDetail.dto.FileReplyExportRequest;
import com.asset.dispoal.file.replyDetail.dto.FileReplyMonthDetailStatistics;
import com.asset.dispoal.file.replyDetail.dto.FileReplyQueryRequest;
import com.asset.dispoal.file.replyDetail.dto.FileReplyRequest;
import com.asset.dispoal.file.replyDetail.dto.FileReplyYearDetailStatistics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import tech.mhuang.pacebox.springboot.protocol.Result;

import javax.servlet.http.HttpServletResponse;

@Service
public class FileReplyDetailServiceImpl implements FileReplyDetailService {

    @Autowired
    private FileReplyPageCommand pageCommand;

    @Autowired
    private FileReplySaveCommand saveCommand;

    @Autowired
    private FileReplyModifyCommand modifyCommand;

    @Autowired
    private FileReplyDeleteCommand deleteCommand;

    @Autowired
    private FileReplyDetailMonthStatisticsCommand detailMonthStatisticsCommand;
    @Autowired
    private FileReplyDetailYearStatisticsCommand detailYearStatisticsCommand;


    @Override
    public Result page(FileReplyQueryRequest request) {
        return pageCommand.execute(request);
    }

    @Override
    public Result save(FileReplyRequest request) {
        return saveCommand.execute(request);
    }

    @Override
    public Result modify(FileReplyRequest request) {
        return modifyCommand.execute(request);
    }

    @Override
    public Result delete(String id) {
        return deleteCommand.execute(id);
    }

    @Autowired
    private Environment environment;

    @Autowired
    private FileReplyDetailExcelDataSaveCommand fileExcelDataSaveCommand;

    @Override
    public void uploadExcel(String filepath) {
        String filePath = environment.getProperty("file.evidence.url") + "/" + filepath;
        ExcelReaderBuilder read = EasyExcel.read(filePath, ReplyDetailExportTemplate.class, new ReplyDetailExcelListener(fileExcelDataSaveCommand));
        read.sheet().doRead();
    }

    @Override
    public Result<FileReplyMonthDetailStatistics> monthDetailStatistics() {
        return detailMonthStatisticsCommand.execute();
    }

    @Override
    public Result<FileReplyYearDetailStatistics> yearDetailStatistics(int year) {
        return detailYearStatisticsCommand.execute(year);
    }

    @Autowired
    private FileReplyExportCommand exportCommand;

    @Override
    public void export(HttpServletResponse response, FileReplyExportRequest request) {
        exportCommand.execute(response, request);
    }
}
